<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="../mastertemplate/MasterPage.xhtml" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
	xmlns:c="http://java.sun.com/jsp/jstl/core">
	<f:metadata>
		<f:viewParam name="selectedRoleId" value="#{UserRolesBean.selectedRoleId}" />
	</f:metadata>
	<ui:define name="left">
		<h:form id="menuForm">
			<ui:insert name="menubar">
				<ui:include src="tools/privilegemenubar.xhtml" />
			</ui:insert>
		</h:form>
	</ui:define>
	<ui:define name="right">
		<h:form id="roleForm">		
			<p:growl id="messages" />
			<p:panel header="#{msg.privilege_title}">
				<p:commandButton rendered="#{UserBean.getAccessLevel() == 'WRITE'}" 
					id="addRole" icon="ui-icon-plusthick" value="#{msg.general_add}" styleClass="tableHeaderButton"
					update=":editForm" action="#{UserRolesBean.addRole}"
					oncomplete="PF('editDialog').show();">
					<f:setPropertyActionListener target="#{UserRolesBean.createNew}" value="#{true}" />
				</p:commandButton>
				<p:tooltip for="addRole" value="#{msg.general_add_tooltip}"/>	
				<p:dataTable var="userRole" value="#{UserRolesBean.userRoles}" paginatorPosition="bottom"
					paginator="true" rows="10" rowsPerPageTemplate="10,20,50">
					<f:facet name="header">	
						<h:outputText value="#{msg.privilege_userroles_header}" styleClass="tableHeader" />
					</f:facet>
					
					<p:column headerText="#{msg.table_roleid}" styleClass="roleSearchColumn" filterBy="#{userRole.roleId}" filterMatchMode="contains"
						sortBy="#{userRole.roleId}">
						<h:outputText value="#{userRole.roleId}" />
					</p:column>
					<p:column headerText="#{msg.table_rolename}" styleClass="roleSearchColumn" filterBy="#{userRole.roleName}" filterMatchMode="contains"
						sortBy="#{userRole.roleName}">
						<h:outputText value="#{userRole.roleName}" />
					</p:column>
					<p:column headerText="#{msg.table_actions}" style="text-align:center; min-width:90px">
						<h:panelGroup>
							<p:commandButton styleClass="smallButton" action="#{UserRolesBean.selectRole(userRole)}"
								update=":roleForm" icon="ui-icon ui-icon-search" id="selectrole"/>
							<p:tooltip for="selectrole" value="#{msg.privilege_role_tooltip}"/>		
							<h:panelGroup rendered="#{UserBean.getAccessLevel() == 'WRITE'}">					
								<p:commandButton styleClass="smallButton" action="#{UserRolesBean.selectRole(userRole)}"
									update=":editForm" icon="ui-icon ui-icon-pencil" oncomplete="PF('editDialog').show();" id="editrole">
									<f:setPropertyActionListener target="#{UserRolesBean.createNew}" value="#{false}" />
								</p:commandButton>
								<p:tooltip for="editrole" value="#{msg.general_edit_tooltip}"/>	
								<p:commandButton styleClass="smallButton" action="#{UserRolesBean.selectRole(userRole)}"
									onclick="PF('confirmDelete').show()" icon="ui-icon ui-icon-trash" id="deleterole"/>
								<p:tooltip for="deleterole" value="#{msg.general_delete_tooltip}"/>	
							</h:panelGroup>		
						</h:panelGroup>
					</p:column>																			
				</p:dataTable>
				
				<h:panelGroup rendered="#{! empty UserRolesBean.selectedRole.roleId}">
					<br />
					<br />
					<p:commandButton rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
						id="saveRight" icon="ui-icon-plusthick" value="#{msg.general_save}" 
						styleClass="tableHeaderButton" update=":roleForm"
						action="#{UserRolesBean.editRightsInRole}"/>
					<p:tooltip for="saveRight" value="#{msg.general_save_tooltip}"/>	
					<p:commandButton rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
						id="refreshRight" icon="ui-icon-refresh" value="#{msg.general_refresh}" 
						styleClass="tableHeaderButton" update=":roleForm"
						action="#{UserRolesBean.refreshRightsWhenScreenChanges}" />
					<p:tooltip for="refreshRight" value="#{msg.general_refresh_tooltip}"/>
				</h:panelGroup>
				
				<p:dataTable id="entryTable" rendered="#{! empty UserRolesBean.selectedRole.roleId}" var="userRight" value="#{UserRolesBean.selectedRights}" 
					paginatorPosition="bottom" paginator="true" rows="10" rowsPerPageTemplate="10,20,50">
					<f:facet name="header">		
						<h:outputText value="#{msg.privilege_userrights_header}: #{UserRolesBean.selectedRole.roleName}" styleClass="tableHeader" />
					</f:facet>				
										
					<p:column headerText="#{msg.table_typeid}" styleClass="roleSearchColumn">
						<h:outputText value="#{userRight.typeId}" />
					</p:column>
					<p:column headerText="#{msg.table_accesslevel}" styleClass="roleSearchColumn">			
						<h:selectOneMenu value="#{userRight.accessLevel}" disabled="#{UserBean.getAccessLevel() != 'WRITE'}">
								<f:selectItems value="#{UserRolesBean.levelList}"/>
						</h:selectOneMenu>						
					</p:column>													
				</p:dataTable>
			</p:panel>
		</h:form>
	</ui:define>
	<ui:define name="content">
		<h:form id="deleteForm">
			<p:confirmDialog header="#{msg.general_delete_header}" id="confirmDelete1" modal="true" widgetVar="confirmDelete" width="450" height="80"
				message="#{msg.confirmDialog_sure}">
				<div style="float: right">
					<p:commandButton value="#{msg.general_ok}" actionListener="#{UserRolesBean.deleteRole}" 
						oncomplete="PF('confirmDelete').hide()" update=":roleForm"/>
					<p:commandButton value="#{msg.general_cancel}" oncomplete="PF('confirmDelete').hide()"/>
				</div>
			</p:confirmDialog>		
		</h:form>

		<p:dialog id="editDialog1" header="#{msg.general_addedit_header}" modal="true" resizable="false"
			widgetVar="editDialog">
			<h:form id="editForm">
				<p:growl id="messages" />
				<h:panelGrid id="editTable" columns="2" styleClass="twoColTableTag twoColTableValue">
					<h:outputText value="#{msg.table_roleid}:"/>
					<h:inputText label="#{msg.table_roleid}" required="true"  value="#{UserRolesBean.selectedRole.roleId}" id="roleId"  
						disabled="#{! empty UserRolesBean.selectedRole.roleId}" style="width:100%; min-width:100px"/>
					<h:outputText value="#{msg.table_rolename}:" />
					<h:inputText label="#{msg.table_rolename}" required="true" id="roleName" value="#{UserRolesBean.selectedRole.roleName}" style="width:100%"/>
				</h:panelGrid>
				<p:commandLink oncomplete="PF('editDialog').hide()" update=":roleForm" rendered="#{!UserRolesBean.createNew}">
							<h:outputText value="#{msg.table_rights}" />
				</p:commandLink>
				<br />
				<div style="float: right">
					<p:commandButton value="#{msg.general_save}"
						action="#{UserRolesBean.saveRole}"
						oncomplete="if (args.opSuccess) PF('editDialog').hide();" update=":roleForm" />
				</div>
			</h:form>
		</p:dialog>			
	</ui:define>

</ui:composition>
